﻿using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class UpdateMgr : MonoBehaviour
{
    /// <summary>
    /// 本地persistentDataPath版本文件路径
    /// </summary>
    public string localVerPath;
    /// <summary>
    /// 本地的版本信息
    /// </summary>
    public Version localVer = new Version();
    /// <summary>
    /// 服务端版本文件路径
    /// </summary>
    public string serverVerPath;
    /// <summary>
    /// 服务器端的版本信息
    /// </summary>
    public Version serverVer = new Version();
    private string latestVerString;
    public UpdateRes updateRes;

    public void Awake()
    {
        localVerPath = Application.persistentDataPath + "/version.txt";
        StartCoroutine(GetLatestVer("https://xuefei.net.cn/HybridCLR/latest.txt"));
    }

    /// <summary>
    /// 获取最新版本信息
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns> 
    IEnumerator GetLatestVer(string url)
    {
        Debug.Log("url:" + url);
        using (UnityWebRequest uwr = UnityWebRequest.Get(url))
        {
            yield return uwr.SendWebRequest();
            if (uwr.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError("GetLatestVer error:" + uwr.error);
                uwr.Dispose();
                //UI提示
                yield break;
            }
            else
            {
                latestVerString = uwr.downloadHandler.text;
                uwr.Dispose();
                serverVer = ReadVersion(latestVerString);
            }
            GetLocalVer(localVerPath);
        }
    }

    /// <summary>
    /// 获取本地版本信息
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    void GetLocalVer(string filePath)
    {
        Debug.Log("localVerPath:" + filePath);
        string localVerString = "";
        if (!File.Exists(localVerPath))
        {
            File.WriteAllText(localVerPath, latestVerString);
        }
        else
        {
            localVerString = File.ReadAllText(localVerPath);
        }
        localVer = ReadVersion(localVerString);
        CheckVer(serverVer, localVer);
    }

    /// <summary>
    /// 检查更新
    /// </summary>
    /// <param name="serverVer"></param>
    /// <param name="localVer"></param>
    /// <param name="OnComplete"></param>
    void CheckVer(Version serverVer, Version localVer)
    {
        Debug.LogWarning("开始检查更新");
        Debug.LogWarning("localVer.AppVer:" + localVer.AppVer
            + " serverVer.AppVer:" + serverVer.AppVer);

        //本地的app版本小于服务器端版本，更新app
        if (localVer.AppVer != 0 && localVer.AppVer < serverVer.AppVer)
        {
            //移动端
            if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
            {

            }
            //PC端
            if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXPlayer)
            {

            }
            //编辑器测试用
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
            {

            }
            return;
        }
        //打开资源检查更新界面
        updateRes.gameObject.SetActive(true);
        updateRes.Init(serverVer.ResVer);
    }

    public static Version ReadVersion(string fileString)
    {
        Version version = new Version();
        try
        {
            JsonUtility.FromJsonOverwrite(fileString, version);
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }
        return version;
    }

    public void Dispose()
    {

    }
}